package com.kczyjg.hybird

import android.content.Context
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener

object GaoDeUtils {
    private lateinit var amapClient: AMapLocationClient
    private lateinit var mContext: Context
    private var locationInfo: DoubleArray = doubleArrayOf(0.0, 0.0)
    fun init(context: Context) {
        this.mContext = context
        //定位的合规接口
        AMapLocationClient.updatePrivacyShow(mContext, true, true)

        //设置是否同意用户授权政策
        AMapLocationClient.updatePrivacyAgree(mContext, true)
        //初始化client
        amapClient = AMapLocationClient(mContext)
        // 设置定位监听
        amapClient.setLocationListener(aMapLocationListener)
        // 设置定位参数
        amapClient.setLocationOption(getOption())
        amapClient.stopLocation()
        amapClient.startLocation()

    }

    fun getOption(): AMapLocationClientOption {
        val mOption = AMapLocationClientOption()
        mOption.locationPurpose = AMapLocationClientOption.AMapLocationPurpose.Sport //使用运动定位场景
        mOption.interval = 1000 //发起定位请求的时间间隔，单位：毫秒
        mOption.isMockEnable = false //禁用位置模拟
        mOption.isLocationCacheEnable = false //关闭缓存机制
        mOption.isBeidouFirst = true //优先使用北斗
        mOption.deviceModeDistanceFilter = 1f //回调最少间隔距离值,单位：米
        mOption.isWifiScan = true //是否允许调用WIFI刷新
        return mOption
    }

    val aMapLocationListener = AMapLocationListener {
        if (it.errorCode == AMapLocation.LOCATION_SUCCESS) {
            //火星坐标系转换
            locationInfo = TransformUtil.gcj02ToGps84(it.latitude, it.longitude)
        } else {
            //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
        }

    }

    fun getLocationInfo(): DoubleArray {
        return locationInfo
    }
}